package com.jp2p.admin;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
@MapperScan(basePackages="com.jp2p.admin.mapper")
public class DatabaseConfiguration {
	
	@Bean
	public DataSource dataSource(){
		
		DataSource dataSource = new DataSource();
		dataSource.setUrl("jdbc:mysql://localhost/jp2p2");
		dataSource.setUsername("root");
		dataSource.setPassword("");
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		return dataSource;
	}
	
	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception{
		
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource());
		return sessionFactory.getObject();
	}
	
	@Bean
	public DataSourceTransactionManager dataSourceTransactionManager(){
		
		return new DataSourceTransactionManager(dataSource());
	}
}
